[2025-07-15] Command-injection-1
๐ฆฅ ๋ณธ๋ฌธ
- app.py
#!/usr/bin/env python3
import subprocess
from flask import Flask, request, render_template, redirect
from flag import FLAG
APP = Flask(__name__)
@APP.route('/')
def index():
return render_template('index.html')
@APP.route('/ping', methods=['GET', 'POST'])
def ping():
if request.method == 'POST':
host = request.form.get('host')
cmd = f'ping -c 3 "{host}"'
try:
output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
return render_template('ping_result.html', data=output.decode('utf-8'))
except subprocess.TimeoutExpired:
return render_template('ping_result.html', data='Timeout !')
except subprocess.CalledProcessError:
return render_template('ping_result.html', data=f'an error occurred while executing the command. -> {cmd}')
return render_template('ping.html')
if __name__ == '__main__':
APP.run(host='0.0.0.0', port=8000)
/ping API์์ ์ฌ์ฉ์๊ฐ ๋ณด๋ธ ํ๋ผ๋ฏธํฐ๋ฅผ ํ
์คํธํ ํ์ฌ ping -c 3
์ ์คํํ๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ โ๋ฅผ ํตํด ํ ์คํธํ๋ฅผ ๋ฒ์ด๋ flag.py๋ฅผ cat์ ํตํด ๋ณผ ์ ์์ ๊ฒ์ด๋ค. ํ์ง๋ง
- ping.html
<input type="text" class="form-control" id="Host" placeholder="8.8.8.8" name="host" pattern="[A-Za-z0-9.]{5,20}" required>
์์ ๊ฐ์ ํํฐ๋ง์ด ์๋ ๋ฐ ๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ํตํด ์ง์์ค ํ ๋ณด๋ธ๋ค
ํ์ด
- ๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ํตํด pattern ๋ถ๋ถ์ ์ง์ด๋ค.
- ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ๋ณด๋ธ๋ค
8.8.8.8"; cat ./flag.py #
Leave a comment